import wepy from 'wepy';

import tip from '@/utils/tip';
import api from '@/api/api';
import {
  TOKEN,
  USER_SPECIAL_INFO
} from '@/utils/constant';

export default class VerificationMixin extends wepy.mixin {
  data = {}

  methods = {}

  async Verification(callback) {

    let res = await wepy.login();
    if (res.code) {

      let rlt = await api.wxJsCode2Session({
        query: { code: res.code },
        tipStart: () => tip.loading(),
        tipEnd: () => tip.loaded()
      });

      if (rlt.data.data) {
        let data = rlt.data.data; //thirdSession

        // let myInfo = wepy.getStorageSync(USER_INFO); //未授权时没有
        let specialInfo = wepy.getStorageSync(USER_SPECIAL_INFO); //未授权时没有

        if (specialInfo) {
          let token = await api.userInfo2Token({
            query: {
              thirdSesson: data,
              rawData: specialInfo.rawData,
              signature: specialInfo.signature,
              encryptedData: specialInfo.encryptedData,
              iv: specialInfo.iv
            }
          });

          wepy.setStorageSync(TOKEN, token.data.data);
          typeof callback === "function" && callback(token);

        }

      }
    }
  }

}
